package com.sl.au.controller;

import com.sl.au.entity.Account;
import com.sl.au.entity.Attachment;
import com.sl.au.entity.ChanceInfo;
import com.sl.au.entity.Module;
import com.sl.au.entity.News;
import com.sl.au.entity.Section;
import com.sl.au.entity.Task;
import com.sl.au.entity.User;
import com.sl.au.repository.AfterSaleTicketRepository;
import com.sl.au.service.NewsService;
import com.sl.au.service.SectionService;
import com.sl.au.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.service.AfterSaleInHouseService;
import com.sl.au.service.AfterSaleOutHouseService;
import com.sl.au.service.AttachmentService;
import com.sl.au.service.IndexService;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/ui/index")
public class IndexController extends BaseController {

	@Autowired
	NewsService newsService;

	@Autowired
	SectionService sectionService;

	@Autowired
	AttachmentService attachmentService;

	@Autowired
	IndexService indexService;

	@Autowired
	UserService userService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = createLayoutView("index/index");
		return modelAndView;
	}

	//// 公司信息发布
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		if (key == null) {
			key = "";
		}
		Module module = this.getPageModule("cir");
		Account a = this.getMyAccount();
		String dataFilter=module.getDataFilter();
		User user = a.getUser();
		String userId=user.getId();
		String superior=user.getSaleArea().getSuperior();
		String areaId=user.getSaleArea().getId();
		condition.put("key", key);
		condition.put("dataFilter", dataFilter);
		condition.put("superior", superior);
		condition.put("userId", userId);
		condition.put("areaId", areaId);
		
		List<News> newsess = null;
		newsess = newsService.getNews(start, length, condition, null, null);
		Long count = newsService.getNewsCount(condition);
		List<News> news1 = new ArrayList<News>();
		for (News n : newsess) {
			String parentId = n.getId();
			List<Attachment> a1 = attachmentService.getRepository().findByParentId(parentId);
			int num = a1.size();
			n.setAttachNumber(num);
			news1.add(n);
		}
		map.put("data", news1);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	@RequestMapping(value = "getMyTaskpage", method = RequestMethod.GET)
	public Map<String, Object> getMyTaskPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		String roleId = a.getRole().getId();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<Task> tasks = new ArrayList<Task>();
		tasks = indexService.getMyTask(start, length, userId, roleId);
		long count = indexService.getRepository().getTaskCount(userId, roleId);
		map.put("data", tasks);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;

	}

	/// 销售机会处理
	@RequestMapping(value = "getChanceDealPage", method = RequestMethod.GET)
	public Map<String, Object> getChanceDealPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<ChanceInfo> chanceInfos = new ArrayList<ChanceInfo>();
		List<ChanceInfo> chanceInfoNew = new ArrayList<ChanceInfo>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		Account a = this.getMyAccount();
		User user = a.getUser();
		String role = a.getRole().getName();
		chanceInfos = indexService.getChanceDealPage(user, role);
		if (chanceInfos != null) {
			for (int i = start; i < start + 5; i++) {
				if (i == chanceInfos.size()) {
					break;
				}
				chanceInfoNew.add(chanceInfos.get(i));
			}
		}
		int count = chanceInfos.size();
		map.put("data", chanceInfoNew);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;

	}

	/**
	 * 添加待办任务:包括 1.售后申请退回 2.售后申请待提交 3.待办派遣单 4.维护超期
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addUndoTask", method = RequestMethod.GET)
	public Map<String, Object> addUndoTask(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String userId = request.getParameter("userId");
		User user = userService.getRepository().findOne(userId);
		Account a = accountService.getRepository().findByUser(user);
		String name = request.getParameter("name");
		String type = request.getParameter("type");
		String content = request.getParameter("content");
		String taskId = request.getParameter("taskId");
		String transactorId = "";
		Task t1 = indexService.getRepository().findByUndoTaskIdAndName(taskId, name);
		if (t1 == null) {
			Task t = new Task();
			if (type.equals("0")) {
				transactorId = userId;
			} else if (type.equals("1")) {
				transactorId = a.getRole().getId();
			}
			t.setTransactorId(transactorId);
			t.setContent(content);
			t.setName(name);
			t.setUndoTaskId(taskId);
			indexService.getRepository().save(t);
			map.put("data", 1);
		} else {
			if (!t1.getTransactorId().equals(userId)) {
				t1.setTransactorId(userId);
				indexService.getRepository().save(t1);
			}
		}
		map.put("data", 0);
		return map;
	}

	/**
	 * 删除待办任务
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/delTask", method = RequestMethod.GET)
	public Map<String, Object> delTask(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String taskId = request.getParameter("taskId");
		String name = request.getParameter("taskName");
		Task task = indexService.getRepository().findByUndoTaskIdAndName(taskId, name);
		if (task != null)
			indexService.getRepository().delete(task);
		map.put("data", 0);
		return map;
	}

	/*
	 * 获得手机CRM显示的数据
	 */
	@RequestMapping(value = "getCount", method = RequestMethod.GET)
	public Map<String, Object> getCount(HttpServletRequest request) {
		Map<String, Object> infos = new HashMap<String, Object>();

		String userId = null;
		String superior = null;
		String areaId = null;
		String sectionId = null;
		String areaName=null;
		String cFilter=null;
		String chanceFilter=null;
		String customerFilter=null;
		Account a = this.getMyAccount();
		if (a != null) {
			User user = a.getUser();
			userId = user.getId();
			superior = a.getUser().getSaleArea().getSuperior();
			areaName = a.getUser().getSaleArea().getAreaName();
			areaId = a.getUser().getSaleArea().getId();
			Section s = sectionService.getRepository().findByChief(a.getUser());
			if (s != null) {
				sectionId = s.getId();
			}
		}
		Module cModule = this.getPageModule("contractInfo");
		if(cModule!=null){
			cFilter=cModule.getDataFilter();
		}
		Module chanceModule = this.getPageModule("chanceQuery");
		if(chanceModule!=null){
			chanceFilter=chanceModule.getDataFilter();
		}
		Module customerModule = this.getPageModule("customerinfo");
		if(customerModule!=null){
			customerFilter=customerModule.getDataFilter();
		}

		Map<String, String> condition = new HashMap<String, String>();
		condition.put("userId", userId);
		condition.put("superior", superior);
		condition.put("areaId", areaId);
		condition.put("sectionId", sectionId);
		condition.put("areaName", areaName);
		condition.put("cFilter", cFilter);
		condition.put("chanceFilter", chanceFilter);
		condition.put("customerFilter", customerFilter);
		infos = indexService.newAddCount(condition);
		return infos;
	}
}
